/**
 * 
 */
package com.zznode.client.wlanpolling.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

/**
 * @author Bean
 * 
 */
public class SignalView extends View {
	private int signal = -74;
	private Paint backGroundPainter;
	private Paint forgroundPainter;

	public SignalView(Context context) {
		super(context);

		init();
	}

	public SignalView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);

		init();
	}

	public SignalView(Context context, AttributeSet attrs) {
		super(context, attrs);

		init();
	}

	private void init() {
		backGroundPainter = new Paint();
		backGroundPainter.setColor(Color.WHITE);
		backGroundPainter.setAntiAlias(true);
		backGroundPainter.setStrokeWidth(1);
		backGroundPainter.setStyle(Paint.Style.STROKE);
		
		forgroundPainter = new Paint();
		forgroundPainter.setColor(Color.GREEN);
		forgroundPainter.setAntiAlias(true);
		forgroundPainter.setStrokeWidth(1);
		forgroundPainter.setStyle(Paint.Style.FILL);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		
		int roundRectWidth = getWidth() - 2;
		RectF rect = new RectF(1, 1, roundRectWidth, getHeight() - 2);
		canvas.drawRoundRect(rect, 2, 2, backGroundPainter);
		
		int lengthWidth = roundRectWidth*(signal + 100)/50;
		
		RectF rect1 = new RectF(1, 1, lengthWidth, getHeight() - 2);
		canvas.drawRoundRect(rect1, 2, 2, forgroundPainter);
	}

	public void setSignal(int signal) {
		if (signal > -50)
			signal = -50;
		if (signal < -100)
			signal = -100;
		
		this.signal = signal;
	}
}
